<?php

class CoffeeShopImage extends CActiveRecord
{
	public static $categories=array('0'=>'Public','1'=>'Top100');
	
	public $imageUpload;
	public $thumbUpload;
	/**
	 * The followings are the available columns in table '{{CoffeeShopImage}}':
	 * @var integer $id
	 * @var integer $shopId
	 * @var string $title
	 * @var string $description
	 * @var string $filePath
	 * @var string $thumbPath
	 * @var integer $uploadedBy
	 * @var integer $uploadTime
	 * @var string $uploadIp
	 * @var integer $priority
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{CoffeeShopImage}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
		array('shopId, filePath, thumbPath, uploadedBy, uploadTime, uploadIp', 'required'),
		array('shopId, uploadedBy, uploadTime, priority,categoryId', 'numerical', 'integerOnly'=>true),
		array('title, description, filePath, thumbPath', 'length', 'max'=>255),
		array('uploadIp', 'length', 'max'=>128),
		array('thumbUpload', 'safe'),
		array('categoryId','default','value'=>'0','setOnEmpty'=>true),
		
		// The following rule is used by search().
		// Please remove those attributes that should not be searched.
		array('id, shopId, title, description, filePath, thumbPath, uploadedBy, uploadTime, uploadIp, priority', 'safe', 'on'=>'search'),
		array('imageUpload', 'file', 'types'=>'jpg, gif, png'),
			
			
		);
	}

	public function scopes()
	{
		return array(
            'publicOnly'=>array(
                'condition'=>'categoryId=0',
			),

            	);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'coffeeShop' => array(CActiveRecord::BELONGS_TO, 'CoffeeShop', 'shopId'),
			'uploadedByUser' => array(CActiveRecord::BELONGS_TO, 'User', 'uploadedBy'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'shopId' => 'Shop',
			'title' => 'Title',
			'description' => 'Description',
			'filePath' => 'File Path',
			'thumbPath' => 'Thumb Path',
			'uploadedBy' => 'Uploaded By',
			'uploadTime' => 'Upload Time',
			'uploadIp' => 'Upload Ip',
			'priority' => 'Priority',
			'categoryId'=>'Category'
		);
	}

	public function searchByShop($shopId)
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;
		$criteria->compare('shopId',$shopId);
		$criteria->order='priority DESC';


		return new CActiveDataProvider('CoffeeShopImage', array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);

		$criteria->compare('shopId',$this->shopId);

		$criteria->compare('title',$this->title,true);

		$criteria->compare('description',$this->description,true);

		$criteria->compare('filePath',$this->filePath,true);

		$criteria->compare('thumbPath',$this->thumbPath,true);

		$criteria->compare('uploadedBy',$this->uploadedBy);

		$criteria->compare('uploadTime',$this->uploadTime);

		$criteria->compare('uploadIp',$this->uploadIp,true);

		$criteria->compare('priority',$this->priority);

		return new CActiveDataProvider('CoffeeShopImage', array(
			'criteria'=>$criteria,
		));
	}
}